#!/bin/bash
export PATH=$PATH:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/java/latest/bin:/usr/java/latest/jre:/root/bin

. /etc/init.d/functions

#升级文件全路径
Full_Src_package=$1
#echo "Full_Src_package": ${Full_Src_package}

#生产实例路径
#Instance_Path=$2
#echo "Instance_Path" : ${Instance_Path}

if (( $# == 1 )) && [[ ${Full_Src_package} =~ '/home/rduser' ]] || [[ ${Full_Src_package} =~ '/home/backup' ]] ; then
	echo -e "\033[35m-----------------------update begin------------------------------------ \033[0m"
else
	echo "invalid arguments"
	echo "Usage: sh new_update Full_Src_package"
	echo "Radius_webservice_dir=/home/Tomcat/webapps"
	echo "DB_webservice_dir=/home/Tomcat/webapps"
	echo "Portal_dir=/opt/tomcat-instances/portal/webapps"
	echo "E.g  sh new_update /home/rduser/Portal_update/portal.war"
	exit 1	
fi

#截取升级文件目录
Full_Src_package_dir=`dirname ${Full_Src_package}`
#echo "Full_Src_package_dir": ${Full_Src_package_dir}

#截取带后缀升级文件名
File_name=`basename ${Full_Src_package}`
#echo "File_name": ${File_name}

#截取不带后缀名的文件
File_name1=${File_name%%.*}
#echo File_name1: ${File_name1}

#截取后缀名
File_name_ext=${File_name##*.}
#echo File_name_ext: ${File_name_ext}

Backup_dir_root="/home/backup"

Backup_dir=${Backup_dir_root}/${File_name1}_`(date +%Y%m%d%H%M)`

#####################################函数#########################################
#检验md5文件
check_md5(){
	if [ -e ${File_name1}.md5 ] && [ -e ${File_name1}.war ]; then
		echo "------------------file list---------------------"
		ls -lh
		echo "------------------------------------------------"
		md5sum -c ${File_name1}.md5 &> /dev/null
		if [ $? -ne 0 ]; then
			echo `date "+%Y-%m-%d %H:%M:%S"` "${File_name1}.md5 md5sum check is not ok" >> /home/rduser/Deploy.log
			echo "${File_name1}.md5 md5sum check is not ok"
			exit 1
		else
			echo `date "+%Y-%m-%d %H:%M:%S"` "${File_name1}.md5 md5sum check is ok" >> /home/rduser/Deploy.log
			echo "${File_name1}.md5 md5sum check is ok"
		fi
	fi
}

#根据升级文件格式来判断md5验证逻辑
check_ext_md5(){
	case ${File_name_ext} in
	"gz")
		cd ${Full_Src_package_dir} && echo `date "+%Y-%m-%d %H:%M:%S"` "${File_name} is tar.gz , begin unzip the file" >> /home/rduser/Deploy.log
		[ -d ${File_name1} ] && \mv ${File_name1} ${File_name1}.`date +%Y%m%d%H%M`
		tar xfz ${File_name}
		cd ${File_name1}
		check_md5
			;;
	"war")
		cd ${Full_Src_package_dir}  && echo `date "+%Y-%m-%d %H:%M:%S"` "${File_name} is war" >> /home/rduser/Deploy.log
		check_md5
			;;
		*)
		echo "${Full_Src_package} is not in support list"
		echo `date "+%Y-%m-%d %H:%M:%S"` "${Full_Src_package} is not in support list" >> /home/rduser/Deploy.log
		exit 1
	esac	
}

#关闭tomcat
Kill_tomcat_Service(){
	pid_Count=`ps -ef | grep "${BASE_Dir}" | grep -v grep | wc -l`
	case ${pid_Count} in
		1 )
		while ((${pid_Count}==1)); do
			${Kill_COMM}
			sleep 5s
			pid_Count=`ps -ef | grep ${BASE_Dir} | grep -v grep | wc -l`
		done
		echo `date "+%Y-%m-%d %H:%M:%S"` "${Deploy_type} has been stop" >> /home/rduser/Deploy.log
		echo "${Deploy_type} has been stop"								
			;;
		* )
		echo `date "+%Y-%m-%d %H:%M:%S"` "${Deploy_type} pid_count is not 1" >> /home/rduser/Deploy.log
		echo "${Deploy_type} pid_count is not 1"
		exit 1
	esac	
}

#启动tomcat
Start_tomcat_Service(){
	pid_Count=`ps -ef | grep ${BASE_Dir} | grep -v grep | wc -l`
	case ${pid_Count} in
		0 )
		while ((${pid_Count}==0)); do
			${Start_COMM1}
			sleep 10s
			pid_Count=`ps -ef | grep ${BASE_Dir} | grep -v grep | wc -l`
		done
		echo `date "+%Y-%m-%d %H:%M:%S"` "${Deploy_type} has been start" >> /home/rduser/Deploy.log
		echo "${Deploy_type} has been start"
		ps -ef | grep ${BASE_Dir} | grep -v grep	
			;;
		*)
		echo `date "+%Y-%m-%d %H:%M:%S"` "${Deploy_type} is already running" >> /home/rduser/Deploy.log
		echo "${Deploy_type} is already running"
		exit 1		
	esac
}

#备份原来的实例
Backup_tomcat(){
	if [ -e ${Instance_Path}/${File_name1}.war ]; then
		mkdir -p ${Backup_dir}
 		\mv ${Instance_Path}/${File_name1}.war ${Backup_dir}
 		echo `date "+%Y-%m-%d %H:%M:%S"` "${File_name1}.war has been move to ${Backup_dir}">> /home/rduser/Deploy.log
 		echo "${File_name1}.war has been move to ${Backup_dir}"
		if [ -d ${Instance_Path}/${File_name1} ]; then
			\mv ${Instance_Path}/${File_name1} ${Backup_dir}
			echo `date "+%Y-%m-%d %H:%M:%S"` "${File_name1} has been move to ${Backup_dir}">> /home/rduser/Deploy.log
			echo "${File_name1} has been move to ${Backup_dir}"
		fi
 	else
 		echo `date "+%Y-%m-%d %H:%M:%S"` "${File_name1}.war is not found" >> /home/rduser/Deploy.log
 		echo "${File_name1}.war is not found"
 		exit 1
	fi
}

#更新新的实例
Update_tomcat(){
	case ${File_name_ext} in
		"gz" )
			cd ${Full_Src_package_dir}/${File_name1}
			rsync -avz ${File_name1}.war ${Instance_Path}
			echo `date "+%Y-%m-%d %H:%M:%S"` "${Full_Src_package_dir}/${File_name1}/${File_name1}.war will be rsync to ${Instance_Path}" >> /home/rduser/Deploy.log
			echo "${Full_Src_package_dir}/${File_name1}/${File_name1}.war will be rsync to ${Instance_Path}"
			;;
		"war" )
			rsync -avz ${Full_Src_package} ${Instance_Path}/
			echo `date "+%Y-%m-%d %H:%M:%S"` "${Full_Src_package} will be rsync to ${Instance_Path}" >> /home/rduser/Deploy.log
			echo "${Full_Src_package} will be rsync to ${Instance_Path}"
			;;
	esac
}

#####################################主程序########################################
#判断更新的服务
if [ -f ${Full_Src_package} ]; then
	echo `date "+%Y-%m-%d %H:%M:%S"` "Full_Src_package is " ${Full_Src_package} >> /home/rduser/Deploy.log
	echo "Full_Src_package is " ${Full_Src_package}	
	Deploy_type=0
	BASE_Dir=0
	ls ${Full_Src_package} | grep 'iGuangdong_RadiusWebService.war' > /dev/null && \
	Deploy_type="Radius_webservice" && \
	BASE_Dir='Dcatalina.base=/home/Tomcat' && \
	Kill_COMM="kill -9 `ps -ef | grep ${BASE_Dir} | grep -v grep | awk '{print $2}'`" && \
	Start_COMM1='sh /home/Tomcat/bin/startup.sh' && \
	Instance_Path='/home/Tomcat/webapps'

	ls ${Full_Src_package} | grep 'IGDDBWebService.war' > /dev/null && \
	Deploy_type="IGDDBWebService" && \
	BASE_Dir='Dcatalina.base=/home/Tomcat' && \
	Kill_COMM="kill -9 `ps -ef | grep ${BASE_Dir} | grep -v grep | awk '{print $2}'`" && \
	Start_COMM1='sh /home/Tomcat/bin/startup.sh' && \
	Instance_Path='/home/Tomcat/webapps'

	ls ${Full_Src_package} | grep 'IGDSiteWebService.war' > /dev/null && \
	Deploy_type="IGDSiteWebService.war" && \
	BASE_Dir='Dcatalina.base=/home/Tomcat' && \
	Kill_COMM="kill -9 `ps -ef | grep ${BASE_Dir} | grep -v grep | awk '{print $2}'`" && \
	Start_COMM1='sh /home/Tomcat/bin/startup.sh' && \
	Instance_Path='/home/Tomcat/webapps'

	ls ${Full_Src_package} | grep 'portal.war' > /dev/null && \
	Deploy_type="portal" && \
	BASE_Dir='Dcatalina.base=/opt/tomcat-instances/portal' && \
	Kill_COMM="/etc/init.d/tomcat-portal stop" && \
	Start_COMM1="/etc/init.d/tomcat-portal start" && \
	Instance_Path='/opt/tomcat-instances/portal/webapps'

	ls ${Full_Src_package} | grep 'pizzaexpress.war' > /dev/null && \
	Deploy_type="pizzaexpress" && \
	BASE_Dir='Dcatalina.base=/opt/tomcat-instances/bundle' && \
	Kill_COMM="/etc/init.d/tomcat-bundle stop" && \
	Start_COMM1="/etc/init.d/tomcat-bundle start" && \
	Instance_Path='/opt/tomcat-instances/bundle/webapps'

	ls ${Full_Src_package} | grep 'gzlib.war' > /dev/null && \
	Deploy_type="gzlib" && \
	BASE_Dir='Dcatalina.base=/opt/tomcat-instances/bundle' && \
	Kill_COMM="/etc/init.d/tomcat-bundle stop" && \
	Start_COMM1="/etc/init.d/tomcat-bundle start" && \
	Instance_Path='/opt/tomcat-instances/bundle/webapps'

	ls ${Full_Src_package} | grep 'gwifi.war' > /dev/null && \
	Deploy_type="gwifi" && \
	BASE_Dir='Dcatalina.base=/opt/tomcat-instances/bundle' && \
	Kill_COMM="/etc/init.d/tomcat-bundle stop" && \
	Start_COMM1="/etc/init.d/tomcat-bundle start" && \
	Instance_Path='/opt/tomcat-instances/bundle/webapps'		
else
	echo "${Full_Src_package}: No such file or directory"
	exit 1
fi

if [ -d ${Instance_Path} ]; then
	echo `date "+%Y-%m-%d %H:%M:%S"` "tomcat dir is ${Instance_Path}"  >> /home/rduser/Deploy.log
	echo "tomcat dir is ${Instance_Path}"
else
	echo `date "+%Y-%m-%d %H:%M:%S"` "tomcat dir is not found"  >> /home/rduser/Deploy.log
	echo "tomcat dir is not found"
	exit 1	
fi

[ -d ${Backup_dir_root} ] || mkdir -p ${Backup_dir_root}

#调用函数执行更新
case ${Deploy_type} in
"Radius_webservice" | "IGDDBWebService" | "pizzaexpress" | "gzlib" | "gwifi" | "portal" | "IGDSiteWebService.war")
	echo `date "+%Y-%m-%d %H:%M:%S"` "Deply_type is ${Deploy_type}," "the filename is ${Full_Src_package}">> /home/rduser/Deploy.log
	echo "Deply_type is ${Deploy_type}," "the filename is ${Full_Src_package}"
	check_ext_md5
	Kill_tomcat_Service
	Backup_tomcat
	Update_tomcat
	Start_tomcat_Service
		;;
*)
	echo `date "+%Y-%m-%d %H:%M:%S"` "Deply_type is not found">> /home/rduser/Deploy.log
	echo "Deply_type is not found"
	exit 1
esac

#是否删除回滚前的一个包
if [[ ${Full_Src_package} =~ '/home/backup' ]]; then
	#`echo ${File_name} | awk -F'.' '{print $1}'`
	ls -ltd /home/backup/`echo ${File_name} | awk -F'.' '{print $1}'`*
	echo -e "\033[35m---------------------是否删除回滚前的一个包---------------------------------- \033[0m"
	ls -ltd /home/backup/`echo ${File_name} | awk -F'.' '{print $1}'`* |  head -1 | awk '{print $9}'
fi



